package com.h4313.ghome.ui.fragments;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.h4313.ghome.GHomeApplication;
import com.h4313.ghome.R;
import com.h4313.ghome.io.NetworkHelper;
import com.h4313.ghome.io.ServerUtilities;
import com.h4313.ghome.utils.UIUtils;

public class SecurityFragment extends ModuleFragment implements Refreshable, OnClickListener {

	ImageView mImageHouse;
	TextView mTextHouse;
	Button mUnlock;
	ProgressBar mRefreshing;
	
	public static SecurityFragment newInstance(String name, int id) {

		SecurityFragment frag = new SecurityFragment();
		Bundle b = new Bundle();
		b.putInt(PARAM_ID, id);
		b.putString(PARAM_NAME, name);
		frag.setArguments(b);
		return frag;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_security, container, false);
		
		mImageHouse = (ImageView) v.findViewById(R.id.img_state_house);
		mTextHouse = (TextView) v.findViewById(R.id.tv_state_house);
		mRefreshing = (ProgressBar) v.findViewById(R.id.refresh);
		mUnlock = (Button) v.findViewById(R.id.bt_unlock);
		mUnlock.setOnClickListener(this);
		return v;
	}
	
	@Override
	public void onStart() {
		super.onStart();
		refresh();
	}
	
	@Override
	public void refresh() {
		new AsyncTask<Void, Void, Boolean>() {
			protected void onPreExecute() {
				mRefreshing.setVisibility(View.VISIBLE);
			};
			
			@Override
			protected Boolean doInBackground(Void... params) {
				
				try {
					String res = NetworkHelper.downloadUrl(GHomeApplication.SERVER_URL + "/security/config/"+getModuleId(), 10000, 10000);
					
					JSONObject objRes = new JSONObject(res);
					boolean secured = objRes.getBoolean("secured");
					
					return secured;
				} catch (IOException e) {
					e.printStackTrace();
				} catch (JSONException e) {
					e.printStackTrace();
				}
				
				return false;
			}
			
			protected void onPostExecute(Boolean result) {
				mRefreshing.setVisibility(View.INVISIBLE);
				bindView(result);
			};
			
		}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
		
		
	}
	
	private void bindView(boolean houseSecured) {
		if (houseSecured) {
			mImageHouse.setImageResource(R.drawable.lock);
			mTextHouse.setText(R.string.house_safe);
			mUnlock.setVisibility(View.VISIBLE);
		} else {
			mImageHouse.setImageResource(R.drawable.unlock);
			mTextHouse.setText(R.string.house_unsafe);
			mUnlock.setVisibility(View.INVISIBLE);
		}
		
	}

	@Override
	public void onClick(View view) {
		if (view.getId() == R.id.bt_unlock) {
			
			new AsyncTask<Void, Void, Boolean> () {

				@Override
				protected Boolean doInBackground(Void... params) {
					
					Map<String, String> paramMap = new HashMap<String, String>();
					paramMap.put("secured", "false");
					
					try {
						String res = ServerUtilities.put(GHomeApplication.SERVER_URL+"/security/config/"+getModuleId(), paramMap);
						
						JSONObject objRes = new JSONObject(res);
						String code = objRes.getString("code");
						
						return "OK".equals(code);
						
					} catch (IOException e) {
						e.printStackTrace();
					} catch (JSONException e) {
						e.printStackTrace();
					}
					
					return false;
				}
				
				protected void onPostExecute(Boolean result) {
					if (result) {
						UIUtils.showToast(SecurityFragment.this, "Sécuritée désactivée !", Toast.LENGTH_SHORT);
						bindView(false);
					} else {
						UIUtils.showToast(SecurityFragment.this, "Une erreur est survenue", Toast.LENGTH_SHORT);
					}
				};
				
			}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
			
		}
	}

}
